/**
 * @param {number[]} nums
 * @return {number}
 */
var maxSubarraySumCircular = function (nums) {
  let max = [nums[0]],
    min = [nums[0]],
    maxValue = nums[0],
    minValue = nums[0];
  const sum = nums.reduce((pre, cur) => {
    return pre + cur;
  });
  for (let i = 1; i < nums.length; i++) {
    max[i] = Math.max(nums[i], max[i - 1] + nums[i]);
    min[i] = Math.min(nums[i], min[i - 1] + nums[i]);
    maxValue = Math.max(maxValue, max[i]);
    minValue = Math.min(minValue, min[i]);
  }
  return Math.max(maxValue, sum != minValue ? sum - minValue : minValue);
};
let nums = [1, -2, 3, -2];
console.log(maxSubarraySumCircular(nums));
nums = [5, -3, 5];
console.log(maxSubarraySumCircular(nums));
nums = [3, -2, 2, -3];
console.log(maxSubarraySumCircular(nums));
nums = [-3, -2, -3];
console.log(maxSubarraySumCircular(nums));
